<?php
require_once dirname(__FILE__) . '/../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../incls/php/config.php';

session_name(SESSION);
session_start();

$myPdo = DbUtils::createPdoInst();

$order_id = MiscUtils::getParam('id', 0);
$secret_pass = MiscUtils::getParam('secret_pass', 1);

$cond_vals = new stdClass();
$cond_vals->c = 't.order_number = :v1';
$cond_vals->v = array(':v1' => $order_id);
$tmp = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_order', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;


if (count($tmp) == 0) {
  die('Order is not found');
}

if ($secret_pass == 1 && ((isset($_SESSION['index' . USER]) && $tmp[0]->customer_id != $_SESSION['index' . USER]->id && $tmp[0]->customer_id != 0) || 
    (!isset($_SESSION['index' . USER]) && $tmp[0]->customer_id != 0))) {
  echo "
  <script>
  parent.parent.location.href = 'http://" . URL . "?i=" . LOGIN_ID . "&url=' + encodeURIComponent(parent.parent.document.URL);
  </script>
  ";
  die('');
}

$my_order = $tmp[0];

$profile = new stdClass();
$tmp = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'user_profile', NULL, NULL, NULL, NULL, NULL, NULL)->d;
if (count($tmp) > 0) {
  $profile = $tmp[0];
}

$cond_vals = new stdClass();
$cond_vals->c = 't.order_id = :v1';
$cond_vals->v = array(':v1' => $my_order->id);
$purchases = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_purchase', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;

$status = 'Unpaid';
if ($my_order->is_paid == 1) {
  $status = 'Paid';
}
if ($my_order->is_delivered == 1) {
  $status = 'Shipped';
} 
if ($my_order->is_delivered == 1 && (time() - strtotime($my_order->date_deliver)) > 864000) {
  $status = 'Completed';
}
?>
<!DOCTYPE html>
<html>
<head>
<style>
* {
  font-family: Trebuchet MS;
  font-size: 12px;
}
div.wrap {
  border: 1px solid #666;
  padding: 5px;
  max-width: 950px;
}
div.wrap h3 {
  font-size: 16px;
  margin: 0;
}
table.lo {
  width: 100%;
  border-top: 1px solid #666;
  margin-top: 5px;
}
table.lo td {
  vertical-align: top;
}
table.form {
  background-color: #fff;
  border-top: 1px solid #aaa;
  border-left: 1px solid #aaa;
  width: 100%;
}
table.form td {
  padding: 3px 5px; 
  vertical-align: top;
}
table.form td.title {
  background-color: #ddd !important;
  border-bottom: 1px solid #aaa;
  border-right: 1px solid #aaa;
  width: 100px;
}
table.form td.content {
  padding: 0 0 0 5px;
  border-bottom: 1px solid #aaa;
  border-right: 1px solid #aaa;
  vertical-align: middle !important;
}
table.list {
  border-left: 1px solid #aaa;
  border-top: 1px solid #aaa;
  font-size: 13px;
  width: 100%;
}
table.list th.sort {
  text-align: left;
  height: 28px;
  background-color: #ddd;
  padding: 0 5px; 
  border-right: 1px solid #aaa;
  border-bottom: 1px solid #aaa;
  font-weight: bold;
  vertical-align: middle;
  cursor: pointer;
  color: #333;
}
table.list tr.rowodd td {
  padding: 0 5px;
  height: 25px;
  background-color: #eee;
  border-right: 1px solid #aaa;
  border-bottom: 1px solid #aaa;
  word-wrap: break-word;
  vertical-align: middle;
  overflow: hidden;
}
table.list tr.roweven td {
  padding: 0 5px;
  height: 25px;
  background-color: #fff;
  border-right: 1px solid #aaa;
  border-bottom: 1px solid #aaa;
  word-wrap: break-word;
  vertical-align: middle;
}
table.list tr.rowodd:hover td {
  background-color: #f4f3b2;
}
table.list tr.roweven:hover td {
  background-color: #f4f3b2;
}
table.list tr.none td {
  padding: 0 5px;
  height: 25px;
  background-color: #fff;
  border-right: 1px solid #666;
  border-bottom: 1px solid #666;
  word-wrap: break-word;
}
table.list input[type=text] {
  width: 99%;
  border: none;
}

</style>
</head>
<body>
  <div class="wrap">
    <h3><?php echo $profile->shop_name; ?></h3>
    <table class="lo" cellpadding=0 cellspacing=0>
      <tr>
        <td style="width: 60%;">
          <div>&nbsp;</div>
          <b>POST ADDRESS</b>
          <table class="form" cellpadding=0 cellspacing=0>
            <tr>
              <td class="title">First Name</td>
              <td class="content"><?php echo $my_order->first_name; ?>&nbsp;</td>
            </tr>
            <tr>
              <td class="title">Last Name</td>
              <td class="content"><?php echo $my_order->last_name; ?>&nbsp;</td>
            </tr>
            <tr>
              <td class="title">Address1</td>
              <td class="content"><?php echo $my_order->address1; ?>&nbsp;</td>
            </tr>
            <tr>
              <td class="title">Address2</td>
              <td class="content"><?php echo $my_order->address2; ?>&nbsp;</td>
            </tr>
            <tr>
              <td class="title">City</td>
              <td class="content"><?php echo $my_order->city; ?>&nbsp;</td>
            </tr>
            <tr>
              <td class="title">Country</td>
              <td class="content"><?php echo $my_order->country; ?>&nbsp;</td>
            </tr>
            <tr>
              <td class="title">Postal Code</td>
              <td class="content"><?php echo $my_order->zip; ?>&nbsp;</td>
            </tr>
            <tr>
              <td class="title">Phone</td>
              <td class="content"><?php echo $my_order->phone; ?>&nbsp;</td>
            </tr>
            <tr>
              <td class="title">Email</td>
              <td class="content"><?php echo $my_order->email; ?>&nbsp;</td>
            </tr>
          </table>
        </td>
        <td style="padding-left: 10px;width: 40%;">
          <div>&nbsp;</div>
          <b>&nbsp;</b>
          <table class="form" cellpadding=0 cellspacing=0>
            <tr>
              <td class="title">Order No.</td>
              <td class="content"><?php echo $my_order->order_number; ?>&nbsp;</td>
            </tr>
            <tr>
              <td class="title">Date</td>
              <td class="content"><?php echo $my_order->date; ?>&nbsp;</td>
            </tr>
            <tr>
              <td class="title">Payment</td>
              <td class="content"><?php echo $my_order->payment_name; ?>&nbsp;</td>
            </tr>
            <tr>
              <td class="title">Courier</td>
              <td class="content"><?php echo $my_order->delivery_name; ?>&nbsp;</td>
            </tr>
            <?php if ($secret_pass == 1) { ?>
            <tr>
              <td class="title">Status</td>
              <td class="content"><?php echo $status; ?>&nbsp;</td>
            </tr>
            <?php } ?>
          </table>
        </td>
      </tr>
      <tr>
        <td colspan=2>
          <div>&nbsp;</div>
          <b>ORDER DETAILS</b>
          <table class="list" cellpadding=0 cellspacing=0>
            <tr>
              <th class="sort">ITEM</th>
              <th class="sort" style="width: 80px;">QTY</th>
              <th class="sort" style="width: 60px;">EACH</th>
              <th class="sort" style="width: 90px;">TOTAL</th>
            </tr>
            <?php foreach ($purchases as $idx => $pur) { ?>
            <tr class="<?php echo (($idx % 2 == 0) ? 'roweven' : 'rowodd'); ?>">
              <td title="<?php echo $pur->name . (($pur->attr_opt_code != '') ? '(' . $pur->attr_opt_code . ')' : ''); ?>"><?php echo MiscUtils::getShortName($pur->name . (($pur->attr_opt_code != '') ? '(' . $pur->attr_opt_code . ')' : ''), 50); ?></td>
              <td><?php echo $pur->quantity; ?></td>
              <td>$<?php echo $pur->price; ?></td>
              <td id="total<?php echo $idx; ?>">$<?php echo sprintf("%01.2f", $pur->total); ?></td>
            </tr>
            <?php } ?>
            <?php if (count($purchases) == 0) { ?>
            <tr class="none"><td colspan=5>None</td></tr>
            <?php } ?>
          </table>
        </td>
      </tr>
      <tr>
        <td colspan=2>
          <div>&nbsp;</div>
          <b>COMMENTS</b>
          <div><?php echo nl2br($my_order->comment); ?>&nbsp;</div>
        </td>
      </tr>
      <tr>
        <td colspan=2 style="text-align: right">
          <div>&nbsp;</div>
          <div style="margin-left: auto;width: 250px;border: 1px solid #ddd;background: #fff;">
            <div><b>Total: $<?php echo $my_order->total; ?></b></div>
            <div>Delivery Subtotal: $<?php echo $my_order->delivery_subtotal; ?></div>
          </div>
        </td>
      </tr>
    </table>
  </div>
</body>
</html>